package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.RelatedServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.Permit;
import cn.wsalix.admin.entity.RolePermit;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.form.RolePermitForm;
import cn.wsalix.admin.repository.RolePermitRepository;
import cn.wsalix.admin.service.PermitService;
import cn.wsalix.admin.service.RolePermitService;
import cn.wsalix.admin.service.RoleService;

@Service("rolePermitService")
public class RolePermitServiceImpl
		extends
		RelatedServiceImpl<RolePermitForm, RolePermitRepository, RolePermit, SysRole, Permit>
		implements RolePermitService {
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermitService permitService;
	@Autowired
	private RolePermitRepository rolePermitRepository;

	@Override
	public SysRole findPreFix(Long preFixId) {
		return roleService.findById(preFixId);
	}

	@Override
	public Permit findPostFix(Long postFixId) {
		return permitService.findById(postFixId);
	}

	@Transactional
	@Override
	public void removeRelated(RolePermitForm form) {
		if (form.getId() != null) {
			rolePermitRepository.delete(form.getId());
		} else {
			List<RolePermit> roleMenus = findByForm(form);
			rolePermitRepository.delete(roleMenus);
		}

	}

	// @Cacheable(value = "myMenuCache",key = "#userId")
	@Transactional
	@Override
	public RolePermit setRelated(RolePermitForm form) {
		RolePermit rolePermit = new RolePermit();
		rolePermit.setPreFix(findPreFix(form.getPreFixId()));
		rolePermit.setPostFix(findPostFix(form.getPostFixId()));
		rolePermitRepository.save(rolePermit);
		return super.setRelated(form);
	}

	@Override
	public List<RolePermit> findRelateds(RolePermitForm form) {
		if (form.getRoleId() != null && form.getRoleId() != 0) {
			return rolePermitRepository.findsByRoleId(form.getRoleId());
		}
		if (form.getPermitId() != null && form.getPermitId() != 0) {
			return rolePermitRepository.findsByPermitId(form.getPermitId());
		}
		return null;
	}

	@Override
	public List<SysRole> findUnPreFix(Long permitId) {
		return rolePermitRepository.findUnRolesByPermitId(permitId);
	}

	@Override
	public List<Permit> findUnPostFix(Long roleId) {
		return rolePermitRepository.findUnPermitsByRoleId(roleId);
	}

	@Override
	public RolePermitRepository getRepository() {
		return rolePermitRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(RolePermitForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getRoleId() != null && form.getRoleId() != 0) {
			filters.put("preFix.id", new SearchFilter("preFix.id", Operator.EQ,
					form.getRoleId()));
		}
		if (form.getPermitId() != null && form.getPermitId() != 0) {
			filters.put("postFix.id", new SearchFilter("postFix.id", Operator.EQ,
					form.getPermitId()));
		}
		return filters;
	}

	@Override
	public String module() {
		return "/admin/role_permit";
	}

	@Override
	public Class<RolePermit> getEntityClazz() {
		return RolePermit.class;
	}

	@Override
	public List<Permit> findPermitsByUserId(Long userId) {
		return rolePermitRepository.findPermsByUserId(userId);
	}

	@Override
	public List<Permit> findAllPermits() {
		return permitService.findAll();
	}

}
